package com.zego.wawaji.ui.dialog.pay;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.socket.utils.AnimatorUtil;
import com.zego.wawaji.ui.dialog.popup.BottomPopup;

/**
 * 首冲奖励弹框
 * Created by Su on 2018/3/8.
 */
public class PayAwardDialog extends BottomPopup {
    private ObjectAnimator animator;
    private String content;

    public PayAwardDialog(FragmentActivity activity, String content) {
        super(activity);
        setCancelableOutTouch(false);
        this.content = content;
    }

    @Override
    protected View makeContentView() {
        View inflate = LayoutInflater.from(activity).inflate(R.layout.pay_award_dialog, null);

        final ImageView awardImg = (ImageView) inflate.findViewById(R.id.award_img);
        final TextView awardRemark = (TextView) inflate.findViewById(R.id.award_remark);

        awardImg.setOnClickListener(v -> {
            awardImg.setOnClickListener(null);
            animator = AnimatorUtil.tada(awardImg);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.start();

            new android.os.Handler().postDelayed(() -> {
                animator.cancel();
                awardImg.setImageResource(R.drawable.treasure_box_anim);
                AnimationDrawable animationDrawable = (AnimationDrawable) awardImg.getDrawable();
                animationDrawable.start();
                awardRemark.setText(TextUtils.isEmpty(content) ? "恭喜获得一个娃娃，请到我的娃娃中查看" : content);
            }, 2000);
        });

        inflate.findViewById(R.id.btn_confirm).setOnClickListener(v -> dismiss());
        return inflate;
    }
}
